package com.lxmes.business.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.lxmes.base.domain.JcIteminfo;
import com.lxmes.base.mapper.JcIteminfoMapper;
import com.lxmes.business.domain.CcCaigouDetail;
import com.lxmes.business.domain.CcStockinfoInfo;
import com.lxmes.business.mapper.CcStockinfoInfoMapper;
import com.lxmes.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.business.mapper.CcRukuDetailMapper;
import com.lxmes.business.domain.CcRukuDetail;
import com.lxmes.business.service.ICcRukuDetailService;

/**
 * 入库详情Service业务层处理
 * 
 * @author 张志煜
 * @date 2022-07-03
 */
@Service
public class CcRukuDetailServiceImpl implements ICcRukuDetailService 
{
    @Autowired
    private CcRukuDetailMapper ccRukuDetailMapper;
    @Autowired
    private JcIteminfoMapper jcIteminfoMapper;
    @Autowired
    private CcStockinfoInfoMapper stockinfoInfoMapper;

    /**
     * 查询入库详情
     * 
     * @param id 入库详情主键
     * @return 入库详情
     */
    @Override
    public CcRukuDetail selectCcRukuDetailById(Long id)
    {
        return ccRukuDetailMapper.selectCcRukuDetailById(id);
    }

    @Override
    public List<CcRukuDetail> selectCcRukuDetailByDanHao(String danhao) {
        return ccRukuDetailMapper.selectCcRukuDetailByDanHao(danhao);
    }

    /**
     * 查询入库详情列表
     * 
     * @param ccRukuDetail 入库详情
     * @return 入库详情
     */
    @Override
    public List<CcRukuDetail> selectCcRukuDetailList(CcRukuDetail ccRukuDetail)
    {
        return ccRukuDetailMapper.selectCcRukuDetailList(ccRukuDetail);
    }

    @Override
    public List<CcRukuDetail> addRukuItemInfo(Long[] ids) {
        List<CcRukuDetail>  rukuDetailList =new ArrayList<>();
        List<JcIteminfo> jcIteminfoList = jcIteminfoMapper.selecIteminfoByIds(ids);
        if(jcIteminfoList.size()>0){
            for (JcIteminfo info :jcIteminfoList) {
                CcRukuDetail ccRukuDetail=new CcRukuDetail();
                ccRukuDetail.setItemId(info.getItemCode());
                ccRukuDetail.setItemName(info.getItemName());
                ccRukuDetail.setClassId(info.getClassId());
                ccRukuDetail.setClassName(info.getClassName());
                ccRukuDetail.setSpec(info.getSpec());
                ccRukuDetail.setSupplyId(info.getSupplyId());
                ccRukuDetail.setSupplyName(info.getSupplyName());
                ccRukuDetail.setUnit(info.getUnit());
                ccRukuDetail.setRukuNum(new BigDecimal("0"));
                ccRukuDetail.setUnitPrice(new BigDecimal("0"));
                ccRukuDetail.setTotalPrice(new BigDecimal("0"));
                ccRukuDetail.setLot(DateUtils.dateTime());
                CcStockinfoInfo ccStockinfoInfo=new CcStockinfoInfo();
                ccStockinfoInfo.setItemId(info.getItemCode());
                //默认库位
                List<CcStockinfoInfo> stockinfoInfos = stockinfoInfoMapper.selectListinfo(ccStockinfoInfo);
                if(stockinfoInfos.size()>0){
                    ccStockinfoInfo=stockinfoInfos.get(0);
                    ccRukuDetail.setKuquCode(ccStockinfoInfo.getKuquCode());
                    ccRukuDetail.setKuquName(ccStockinfoInfo.getKuquName());
                    ccRukuDetail.setKuweiCode(ccStockinfoInfo.getKuweiCode());
                }
                rukuDetailList.add(ccRukuDetail);
            }
        }
        return rukuDetailList;
    }

    /**
     * 新增入库详情
     * 
     * @param ccRukuDetail 入库详情
     * @return 结果
     */
    @Override
    public int insertCcRukuDetail(CcRukuDetail ccRukuDetail)
    {
        ccRukuDetail.setCreateTime(DateUtils.getNowDate());
        return ccRukuDetailMapper.insertCcRukuDetail(ccRukuDetail);
    }

    /**
     * 修改入库详情
     * 
     * @param ccRukuDetail 入库详情
     * @return 结果
     */
    @Override
    public int updateCcRukuDetail(CcRukuDetail ccRukuDetail)
    {
        ccRukuDetail.setUpdateTime(DateUtils.getNowDate());
        return ccRukuDetailMapper.updateCcRukuDetail(ccRukuDetail);
    }

    /**
     * 批量删除入库详情
     * 
     * @param ids 需要删除的入库详情主键
     * @return 结果
     */
    @Override
    public int deleteCcRukuDetailByIds(Long[] ids)
    {
        return ccRukuDetailMapper.deleteCcRukuDetailByIds(ids);
    }

    /**
     * 删除入库详情信息
     * 
     * @param id 入库详情主键
     * @return 结果
     */
    @Override
    public int deleteCcRukuDetailById(Long id)
    {
        return ccRukuDetailMapper.deleteCcRukuDetailById(id);
    }
}
